package com.coollang.smartbra.fragment;

import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.coollang.smartbra.R;
import com.coollang.smartbra.activity.MainActivity;
import com.coollang.smartbra.base.BaseFragment;
import com.coollang.smartbra.base.MyApplication;
import com.coollang.smartbra.bleutil.BleUtil;
import com.coollang.smartbra.eventbus.CommonEvent;
import com.coollang.smartbra.presenter.FirstFragmentPresenter;
import com.coollang.smartbra.utils.ActivitySwitcher;
import com.coollang.smartbra.utils.PreferencesUtils;
import com.coollang.smartbra.utils.ToastUtils;
import com.coollang.smartbra.utils.TranslucentSystemBarUtils;
import com.coollang.smartbra.utils.UIUtils;
import com.coollang.smartbra.views.MainCircleHelper;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;


/**
 * @author Timmy
 *         首页页面
 */
public class FirstFragment extends BaseFragment implements View.OnClickListener {
    View view;
    @BindView(R.id.tv_title_realtime)
    TextView tvTitleRealtime;
    public static ImageView imgvDeviceRight;
    public static ImageView imgvDeviceRight1;
    // ------------------end ------------\

    //中心圆

    @BindView(R.id.tv_tongbu)
    TextView mTvTongBu;
    @BindView(R.id.iv_tongbu)
    ImageView mIvTongBu;
    @BindView(R.id.action_state)
    TextView mTvActionState;
    @BindView(R.id.action_title)
    TextView mTvActionTitle;

    @BindView(R.id.iv_fist_go)
    ImageView mIvFostGo;

    @BindView(R.id.view_top)
    View viewTop;
    @BindView(R.id.status_bar)
    View statusbar;
    private FirstFragmentPresenter mFragmentPresenter;
    private MainCircleHelper mMainCicleHelper;

    @Override
    protected View inflateView(LayoutInflater inflater) {
        view = inflater.inflate(R.layout.fragment_first_main, null, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Nullable
    @Override
    public View getView() {
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();

        if (MyApplication.getApplication().isConnected) {
            setConnectedRightBtn();
        } else {
            setDisConnectedRightBtn();
        }
    }

    private void setConnectedRightBtn() {
        imgvDeviceRight.setImageResource(R.drawable.device_connect);
        imgvDeviceRight.clearAnimation();
    }

    private void setDisConnectedRightBtn() {
        imgvDeviceRight.setImageResource(R.drawable.device_disconnect);
        imgvDeviceRight.startAnimation(AnimationUtils.loadAnimation(UIUtils.getContext(), R.anim.anim_alpha));
    }

    @Override
    protected void initView(View view) {
        // 设置标题
        tvTitleRealtime.setTextColor(ContextCompat.getColor(getContext(), R.color.black_666));
        TranslucentSystemBarUtils.showChenJinZhuangTaiLan(statusbar);
        findViewByids(view);
    }

    private void findViewByids(View view) {
        imgvDeviceRight1 = (ImageView) view.findViewById(R.id.imgv_device_right);
        imgvDeviceRight1.setImageResource(R.drawable.draw_layout);
        imgvDeviceRight = (ImageView) view.findViewById(R.id.iv_left);
    }

    @Override
    protected void initData() {
        mFragmentPresenter = new FirstFragmentPresenter(this);
        mFragmentPresenter.getMyPlan();
        // 首页圆环
        mMainCicleHelper = new MainCircleHelper(this);
    }

    @Override
    @OnClick({R.id.imgv_device_right, R.id.iv_left, R.id.tv_tongbu, R.id.iv_fist_go})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.imgv_device_right:
                clickIvRight();
                break;
            case R.id.iv_left:
                clickIvLeft();
                break;
            case R.id.iv_fist_go:
            case R.id.tv_tongbu:
                clickCenter();
                break;
            default:
                break;
        }
    }

    /**
     * 点击右侧图标
     */
    private void clickIvRight() {
        if (MyApplication.getApplication().isConnected) {
            // 获取设备电量
            BleUtil.getInstance().sendData((byte) 0x22);

            ((MainActivity) getActivity()).mDrawerLayout.openDrawer(Gravity.RIGHT);
            if (PreferencesUtils.getBoolean(UIUtils.getContext(), "hasNewVersion")) {
                EventBus.getDefault().post(new CommonEvent(true, CommonEvent.REQUESTSUEECCD, CommonEvent.HASNEWVERSION));
            } else {
                EventBus.getDefault().post(new CommonEvent(false, CommonEvent.REQUESTSUEECCD, CommonEvent.HASNEWVERSION));
            }
        } else {
            ((MainActivity) getActivity()).mDrawerLayout.openDrawer(Gravity.RIGHT);
        }
    }

    /**
     * 点击导航栏左侧图标
     */
    private void clickIvLeft() {
        // 判断蓝牙是否连接, 未连接跳转到蓝牙搜索页面,已连接则弹窗
        boolean deviceConnected = BleUtil.getInstance().isDecviceConnected();
        if (deviceConnected) {
            ActivitySwitcher.goToDeviceInfo(getActivity());
        } else {
            // 未连接
            ActivitySwitcher.goSearchAct(getActivity());
        }
    }

    private void clickCenter() {
        int currentItem = mMainCicleHelper.getCurrentItem();
        if (currentItem == 3) {
            if (MyApplication.getApplication().isConnected) {
                // 发送蓝牙消息
                BleUtil.getInstance().sendData((byte) 0x30);
            } else {
                ToastUtils.showToast(UIUtils.getString(R.string.ble_disconnect1));
            }
        } else {
            ActivitySwitcher.goToSportPlan(getActivity());
        }
    }

    public void setTitle(int todayPosition) {
        // 设置标题
        tvTitleRealtime.setText("第" + todayPosition + "/28天");
    }

    public void setTongbuText(String text) {
        mTvTongBu.setText(text);
        mMainCicleHelper.loadProgressSQLData();
    }
}
